﻿Imports System.Xml.Serialization

Public Class CacheConfig
    Inherits DefaultConfig
    Private Shared mConfigInfo As CacheConfigInfo
    Private Shared mFileDateTime As DateTime

    Public Overloads Shared ReadOnly Property ConfigFilePath() As String
        Get
            Dim Path As String = AppSettings("ConfigPath")
            If Not Path.EndsWith("/") Then Path = String.Concat(Path, "/")
            Return System.Web.HttpContext.Current.Server.MapPath(String.Format("{0}cache.config", Path))
        End Get
    End Property

    Shared Sub New()
        Load()
    End Sub

    Public Shared Function Load() As IConfigInfo
        If String.IsNullOrEmpty(ConfigFilePath) OrElse Not IO.File.Exists(ConfigFilePath) Then
            mConfigInfo = New CacheConfigInfo
        Else
            Try
                mConfigInfo = CType(LoadConfig(mFileDateTime, ConfigFilePath, mConfigInfo), CacheConfigInfo)
            Catch ex As Exception
                mConfigInfo = New CacheConfigInfo
            End Try
        End If
        Return mConfigInfo
    End Function

    Public Shared Function Save() As Boolean
        Return SaveConfig(ConfigFilePath, mConfigInfo)
    End Function
End Class

<Serializable()> <XmlRoot("CachePools")> Public Class CacheConfigInfo
    Implements IConfigInfo

    Private CurrentName As String
    Private CurrentDateTime As DateTime
    <XmlElement("CacheItem")> Public Items() As CacheItem

    REM 查找数组
    Public Function Find(ByVal FilePath As String) As CacheItem
        Return Array.Find(Items, AddressOf FindWhere)
    End Function


    Private Function FindWhere(ByVal item As CacheItem) As Boolean
        Return item.XPath = CurrentName
    End Function
End Class

<Serializable()> Public Class CacheItem
    <XmlAttribute("XPath")> Public XPath As String
    <XmlAttribute("XType")> Public XType As String
    <XmlAttribute("FileDateTime")> Public FileDateTime As DateTime
End Class